package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sc.nft.enums.wallet.DepartmentTypeEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.DepartmentWallet;
import com.sc.nft.mapper.DepartmentWalletMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * 部门钱包表(sc_department_wallet)数据DAO
 *
 * @author zxy
 * @since 2023-12-18 18:02:52
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class DepartmentWalletDao extends ServiceImpl<DepartmentWalletMapper, DepartmentWallet> {

    public void addBalance(DepartmentTypeEnum type, BigDecimal amount){
        UpdateWrapper<DepartmentWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("sum_income=sum_income+"+amount+" , balance=balance+"+amount);
        wrapper.eq("department_type",type.getCode());
        update(wrapper);
    }

    public void freeze(DepartmentTypeEnum type, BigDecimal amount){
        UpdateWrapper<DepartmentWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount+"+amount+" , balance=balance-"+amount);
        wrapper.eq("department_type",type.getCode());
        update(wrapper);
    }
    public void subFreeze(DepartmentTypeEnum type, BigDecimal amount){
        UpdateWrapper<DepartmentWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount-"+amount);
        wrapper.eq("department_type",type.getCode());
        update(wrapper);
    }
    public void revoke(DepartmentTypeEnum type, BigDecimal amount){
        UpdateWrapper<DepartmentWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount-"+amount+" , balance=balance+"+amount);
        wrapper.eq("department_type",type.getCode());
        update(wrapper);
    }

    public DepartmentWallet getByType(DepartmentTypeEnum type){
        return lambdaQuery().eq(DepartmentWallet::getDepartmentType,type).one();
    }
}